﻿// node install configPath branch

const mvc   = require('cosjs.mvc');
const cosjs   = require('cosjs');
//============================启动进程=================================//
if(!process.argv[2]){
    return console.log("server setting empty");
}
var option = require(process.argv[2]);
//游戏环境配置
process.env.NODE_ENV = option.debug ? 'development' : 'production';
mvc.config.set(option);
//加载模块
mvc.model.namespace("admin","branch");
//数据库
var pool = cosjs.pool;

Promise.resolve(1).then(()=>{
    return mvc.addPath(option['root']);
}).then(()=>{
    return pool.redis( 'cache',option.dbase.cache);
}).then(()=>{
    return pool.mongodb( 'mongodb',option.dbase.mongodb);
}).then(()=>{
    console.log("start install");
    return install_admin();
}).then(()=>{
    return install_branch();
}).then(()=>{
    console.log('success');
    process.exit();
}).catch(err=>{
    console.log(err);
    process.exit();
})



function install_admin(){
    let guid = "EdroityAdmin";
    let members = mvc.model.admin('members');
    return members.exist(guid).then(ret=>{
        if(ret) {
            return console.log(`user[${guid}] exist`)
        }
        let info = {};
        info['guid'] = guid;
        info['xcode'] = members.password('Edroity@468');
        info['name'] = guid;
        info['time'] = Date.now();
        info['level'] = 100;
        return members.insert(info);
    })
}


function install_branch(){
    if(!process.argv[3]){
        return console.log(`branch empty & nothing to do`);
    }
    let branch = process.argv[3];
    console.log(`add branch[${branch}]`)
    let info = {"_id":branch,"name":branch}
    mvc.format('branch',info,true)
    let model = mvc.model.admin("branch");
    return model.add(info);
}


